package com.findMoneyGame
{
	import com.findMoneyGame.pages.HomePage;
	import com.findMoneyGame.pages.achievement.AchievementWatcher;
	import com.findMoneyGame.pages.preloading.ThePreloader;
	import com.findMoneyGame.models.User;

	import flash.display.MovieClip;
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	public class Engine extends MovieClip {

		public static const LEVELS_PER_SEASON = 20;
		
		private static const gameWidth : int = 800;
		private static const gameHeight : int = 600;
		private var preloader : ThePreloader;

		private function get user () { return User.getInst(); }


		public function Engine() {
			new AchievementWatcher(stage);
			preloader = new ThePreloader(474);
			preloader.addEventListener(ThePreloader.LOAD_COMPLETE, preloadingComplete);
			stage.addChild(preloader);
			preloader.start();
		}
		public static function center (page : DisplayObject, container : DisplayObject = null) {
			var rect = page.getBounds(container == null ? page.stage : container);
			var rectCenterX = rect.x + rect.width / 2;
			var rectCenterY = rect.y + rect.height / 2;
			var containerCenterX = (container == null ? gameWidth : container.width) / 2;
			var containerCenterY = (container == null ? gameHeight : container.height) / 2;
			page.x = page.x + (containerCenterX - rectCenterX);
			page.y = page.y + (containerCenterY - rectCenterY);
		}

		private function preloadingComplete (e : Event) {
			preloader.removeEventListener(ThePreloader.LOAD_COMPLETE, preloadingComplete);
			stage.removeChild(preloader);
			stage.addChild(new HomePage(this));
		}
	}
}